package keqiang.com.kq_flutter_core_widget.channel;

import android.app.Activity;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import keqiang.com.kq_flutter_core_widget.util.ErrCode;
import keqiang.com.kq_flutter_core_widget.util.dingtalk.DDAPI;
import keqiang.com.kq_flutter_core_widget.util.qq.QQAPI;
import keqiang.com.kq_flutter_core_widget.util.wechat.WXAPI;

import java.util.HashMap;
import java.util.Map;

/**
 * flutter 第三方登录/分享通道
 *
 * @author zhouzhuo810
 * @date 2023/3/13 17:49
 */
public class NativeThirdLoginChannel extends MethodChannel implements PluginRegistry.ActivityResultListener {
    
    // 通道id
    private static final String CHANNEL = "flutter.keqiang.com/thirdLogin";
    
    private static final String METHOD_WX_AUTH = "wxAuth";
    private static final String METHOD_DD_AUTH = "ddAuth";
    private static final String METHOD_QQ_AUTH = "qqAuth";
    private static final String METHOD_WX_SHARE_TEXT_DATA = "shareWxTextData";
    private static final String METHOD_WX_SHARE_WEB_DATA = "shareWxWebData";
    private static final String METHOD_WX_SHARE_IMAGE_DATA = "shareWxImageData";
    private static final String METHOD_DD_SHARE_TEXT_DATA = "shareDDTextData";
    private static final String METHOD_DD_SHARE_WEB_DATA = "shareDDWebData";
    private static final String METHOD_DD_SHARE_IMAGE_DATA = "shareDDImageData";
    private static final String METHOD_QQ_SHARE_TEXT_DATA = "shareQQTextData";
    private static final String METHOD_QQ_SHARE_WEB_DATA = "shareQQWebData";
    private static final String METHOD_QQ_SHARE_IMAGE_DATA = "shareQQImageData";
    
    private Activity mActivity;
    private ActivityPluginBinding mBinding;
    
    public NativeThirdLoginChannel(@NonNull BinaryMessenger messenger) {
        super(messenger, CHANNEL);
        
        init();
    }
    
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        mActivity = binding.getActivity();
        mBinding = binding;
        binding.addActivityResultListener(this);
    }
    
    public void onDetachedFromActivity() {
        if (mBinding != null) {
            mBinding.removeActivityResultListener(this);
        }
        mBinding = null;
        mActivity = null;
    }
    
    private void init() {
        setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case METHOD_WX_AUTH:
                    wxAuth(call, result);
                    break;
                
                case METHOD_WX_SHARE_TEXT_DATA:
                    wxShareTextData(call, result);
                    break;
                
                case METHOD_WX_SHARE_WEB_DATA:
                    wxShareWebData(call, result);
                    break;
                
                case METHOD_WX_SHARE_IMAGE_DATA:
                    wxShareImageData(call, result);
                    break;
                
                case METHOD_QQ_AUTH:
                    qqAuth(call, result);
                    break;
                
                case METHOD_QQ_SHARE_TEXT_DATA:
                    qqShareTextData(call, result);
                    break;
                
                case METHOD_QQ_SHARE_WEB_DATA:
                    qqShareWebData(call, result);
                    break;
                
                case METHOD_QQ_SHARE_IMAGE_DATA:
                    qqShareImageData(call, result);
                    break;
                
                case METHOD_DD_AUTH:
                    ddAuth(call, result);
                    break;
                
                case METHOD_DD_SHARE_TEXT_DATA:
                    ddShareTextData(call, result);
                    break;
                
                case METHOD_DD_SHARE_WEB_DATA:
                    ddShareWebData(call, result);
                    break;
                
                case METHOD_DD_SHARE_IMAGE_DATA:
                    ddShareImageData(call, result);
                    break;
            }
        });
    }
    
    @Override
    public boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        return QQAPI.handIntent(requestCode, resultCode, intent);
    }
    
    private void wxAuth(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        WXAPI.authLogin(mActivity, call.argument("appId"), (code, authCode) -> {
            if (code == ErrCode.SUCCESS) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", "0");
                resultMap.put("authCode", authCode);
                result.success(resultMap);
            } else if (code == ErrCode.CANCEL) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", "-1");
                result.success(resultMap);
            } else if (code == ErrCode.APP_NOT_INSTALL) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", "2");
                result.success(resultMap);
            } else {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", "1");
                result.success(resultMap);
            }
        });
    }
    
    private void wxShareTextData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        WXAPI.shareTextData(mActivity, call.argument("appId"), call.argument("content"),
            call.argument("title"), call.argument("desc"), call.argument("icon"), result::success);
    }
    
    private void wxShareWebData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        WXAPI.shareWebData(mActivity, call.argument("appId"), call.argument("url"),
            call.argument("title"), call.argument("desc"), call.argument("icon"), result::success);
    }
    
    private void wxShareImageData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        WXAPI.shareImageData(mActivity, call.argument("appId"), call.argument("localPath"),
            call.argument("bytes"), result::success);
    }
    
    private void ddAuth(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        DDAPI.authLogin(mActivity, call.argument("appId"), (code, authCode) -> {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("code", code);
            resultMap.put("authCode", authCode);
            result.success(resultMap);
        });
    }
    
    private void ddShareTextData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        DDAPI.shareTextData(mActivity, call.argument("appId"), call.argument("content"),
            call.argument("title"), call.argument("msgContent"), call.argument("msgUrl"),
            call.argument("icon"), call.argument("iconUrl"), result::success);
    }
    
    private void ddShareWebData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        DDAPI.shareWebData(mActivity, call.argument("appId"), call.argument("url"),
            call.argument("title"), call.argument("msgContent"), call.argument("msgUrl"),
            call.argument("icon"), call.argument("iconUrl"), result::success);
    }
    
    private void ddShareImageData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        DDAPI.shareImageData(mActivity, call.argument("appId"), call.argument("localPath"),
            call.argument("bytes"), call.argument("url"), result::success);
    }
    
    private void qqAuth(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        try {
            QQAPI.authLogin(mActivity, call.argument("appId"), (code, openId, accessToken) -> {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", code);
                resultMap.put("openId", openId);
                resultMap.put("accessToken", accessToken);
                result.success(resultMap);
            });
        } catch (IllegalArgumentException e) {
            result.error("0", e.getMessage(), e);
        }
    }
    
    private void qqShareTextData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        try {
            QQAPI.shareTextData(mActivity, call.argument("appId"), call.argument("content"), result::success);
        } catch (IllegalArgumentException e) {
            result.error("0", e.getMessage(), e);
        }
    }
    
    private void qqShareWebData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        try {
            QQAPI.shareWebData(mActivity, call.argument("appId"), call.argument("url"),
                call.argument("title"), call.argument("desc"), call.argument("iconUrl"), result::success);
        } catch (IllegalArgumentException e) {
            result.error("0", e.getMessage(), e);
        }
    }
    
    private void qqShareImageData(MethodCall call, @NonNull Result result) {
        if (mActivity == null) {
            result.error("0", "context is null", null);
            return;
        }
        
        try {
            QQAPI.shareImageData(mActivity, call.argument("appId"), call.argument("localPath"), result::success);
        } catch (IllegalArgumentException e) {
            result.error("0", e.getMessage(), e);
        }
    }
}
